# 爬取猫眼电影中榜单栏目中TOP100榜的所有电影信息
import requests
import re
import json

# 保存电影信息 序号、图片、电影名称、主演、时间、评分
def saveMaoyanData(data):
    zffile=open("data_maoyan.txt","w+",encoding="utf-8")
    zffile.write("猫眼电影中榜单栏目中TOP100榜的所有电影信息\n")
    for d in data:
        zffile.write("序号：" + d["number"] + "\n")
        zffile.write("图片：" + d["image"] + "\n")
        zffile.write("电影名称：" + d["name"] + "\n")
        zffile.write("主演：" + d["person"] + "\n")
        zffile.write("时间：" + d["time"] + "\n")
        zffile.write("评分：" + d["score"] + "\n")
    zffile.close()

# 爬取猫眼电影TOP100榜信息
def getMaoyanData():
    myData=[]
    # 请求头信息
    headers = {
        "authority": "maoyan.com",
        "method":"GET",
        # "path": "/ board / 4?offset = 10",
        "scheme": "https",
        "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
        "accept-language": "zh-CN,zh;q=0.9",
        "upgrade-insecure-requests": "1",
        "user-agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36"
    }
    # 分页爬取电影信息
    for i in range(1,11):
        # 定义爬取地址及参数
        offset=(i-1)*10
        url = "https://maoyan.com/board/4?offset="+str(offset)
        try:
            res = requests.get(url,headers=headers)
            data = res.content.decode('utf-8')
            targethtml = re.findall('<dl class="board-wrapper">(.*?)</dl>', data, re.S | re.M)
            if len(targethtml) > 0:
                targetlilist = re.findall('<dd>(.*?)</dd>', targethtml[0], re.S | re.M)
                # 循环解析每条电影信息
                count=0 # 当前页码第几条数据
                for li in targetlilist:
                    count+=1
                    number="" # 序号
                    image = ""  # 图片
                    name = ""  # 电影名称
                    person = ""  # 主演
                    time = ""  # 时间
                    score = ""  # 评分
                    numberlist = re.findall('<i class="board-index board-index-.*?">([0-9]+)</i>', li)
                    if len(numberlist) > 0:
                        number = numberlist[0]
                    imglist = re.findall('<img data-src="(.*?)" alt=".*?" class="board-img" />', li)
                    if len(imglist) > 0:
                        image = imglist[0]
                    namelist = re.findall('<p class="name"><a.*?>(.*?)</a></p>', li)
                    if len(namelist) > 0:
                        name = namelist[0]
                    personlist = re.findall('<p class="star">\s*主演：(.*?)\s*</p>', li)
                    if len(personlist) > 0:
                        person = personlist[0]
                    timelist = re.findall('<p class="releasetime">上映时间：(.*?)</p>', li)
                    if len(timelist) > 0:
                        time = timelist[0]
                    scorelist = re.findall('<p class="score"><i class="integer">(.*?)</i><i class="fraction">(.*?)</i></p>',li)
                    if len(scorelist) > 0:
                        score = scorelist[0][0]+scorelist[0][1]
                    print("第",i,"页信息","第",count,"条数据")
                    print("序号：",number," 图片：",image," 电影名称：",name," 主演：",person," 时间：",time," 评分：",score)
                    myOBJ={"number":number,"image":image,"name":name,"person":person,"time":time,"score":score}
                    myData.append(myOBJ)
            else:
                # 如果没有爬取到信息，显示页面提示
                msg=re.findall('<title>(.*?)</title>',data)
                print(msg)
        except Exception as err:
            print("解析错误",err)
    # 保存电影信息到文件
    print("保存电影信息到文件")
    if len(myData) > 0:
        saveMaoyanData(myData)

if __name__=="__main__":
    getMaoyanData()